/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package utopiaintelpackage.sos;

import main.CommonMethods;
import exceptions.IntelParsingException;

/**
 * This class parses SoSs
 * 
 * 
 */
public class SoSParser {
   private final String sciencesregex = CommonMethods.getScienceRegex();

   public void parseFormattedSoS(final String input, final SoS sos) throws IntelParsingException {
	  String in = input;
	  if (in.matches("Science (Intelligence|Intel) on .*\\([0-9]{1,2}(:)[0-9]{1,2}\\)")) {
		 in = in.replaceFirst("Science (Intelligence|Intel) on ", "");

		 String prov = in.substring(0, in.indexOf('(')).trim();
		 String kd = (in.substring(in.indexOf('('), in.indexOf(')')) + ")").trim();
		 sos.setName(prov);
		 sos.setKdloc(kd);
	  } else if (in.matches("[0-9.]{1,}% " + sciencesregex + ".*") || in.matches("[0-9.]{1,}% " + sciencesregex)) {
		 String effect = in.substring(0, in.indexOf(' '));
		 String type = in.replace(effect, "").trim();
		 String temp = type.replaceAll(sciencesregex, "");
		 type = type.replace(temp, "").trim();
		 type = CommonMethods.getSciencesMapping(type);
		 String points = temp.replaceAll("[^0-9]{1,}", "");
		 if ("".equals(points)) {
			points = "0";
		 }
		 sos.setScienceEffect(type, effect);
		 if (!"".equals(points)) {
			sos.setSciencePoints(type, Integer.parseInt(points));
		 }
	  } else if (in.matches("Land: [0-9,]{1,} Acres")) {
		 sos.setLand(Integer.parseInt(in.replaceAll("[^0-9]", "")));
	  }
   }

   public boolean parseUnformattedSoS(final String input, final SoS sos) throws IntelParsingException {
	  final int size = sos.getIntel().size();
	  final String sciencesregex = CommonMethods.getScienceRegex();
	  String in = input;
	  if (in.matches("Our thieves visit the research centers of " + ".*\\([0-9]{1,2}(:)[0-9]{1,2}\\).*")) {
		 in = in.replace("Our thieves visit the research centers of ", "");

		 final String prov = in.substring(0, in.indexOf('(')).trim();
		 final String kd = (in.substring(in.indexOf('('), in.indexOf(')')) + ")").trim();
		 sos.setName(prov);
		 sos.setKdloc(kd);
	  } else if (in.matches(sciencesregex + "\t[0-9,]{1,}\t[0-9.]{1,}\t\\+[0-9.]{1,}% .*")) {
		 final String[] temp = in.split("\t");
		 final String points = temp[1].replace(",", "").trim();
		 String effect = temp[3].replace("+", "");
		 effect = effect.substring(0, effect.indexOf(' '));
		 sos.setSciencePoints(temp[0].trim(), Integer.parseInt(points.replace(" ", "")));
		 sos.setScienceEffect(temp[0].trim(), effect.trim());
	  }
	  return size < sos.getIntel().size();
   }
}
